﻿Imports wsConsorcio = BCS.SOAP2012.FrontEnd.wsSOAP2012

Public Class frmConfirmacion
    Inherits frmMaster

    Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            lblRUTCliente.Text = RUTCliente
            lblNombre.Text = NombreCliente
            lblEmail.Text = Email

            lblPatente.Text = Patente
            lblTipoVehiculo.Text = TipoVehiculo
            lblMarca.Text = Marca
            lblModelo.Text = Modelo
            lblAnno.Text = Anio
        End If
    End Sub

    Protected Sub btnVolver_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnVolver.Click
        IrA("frmListadoSeguros.aspx")
    End Sub

    Private Sub EnviarMail(ByVal ColorCliente As String, ByVal Cliente As String, ByVal Patente As String, ByVal Para As String)
        'create the mail message
        Dim mail As New Net.Mail.MailMessage()

        'set the addresses
        If ConfigurationManager.AppSettings("Correo.De").Split("|").Length = 2 Then
            mail.From = New Net.Mail.MailAddress(ConfigurationManager.AppSettings("Correo.De").Split("|")(0), ConfigurationManager.AppSettings("Correo.De").Split("|")(1))
        Else
            mail.From = New Net.Mail.MailAddress(ConfigurationManager.AppSettings("Correo.De"))
        End If

        For Each c As String In Para.Split(",")
            mail.To.Add(c)
        Next

        'set the content
        mail.Subject = ConfigurationManager.AppSettings("Correo.Asunto")

        ''first we create the Plain Text part
        'Dim plainBody As String = Mensaje
        'Dim plainView As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(plainBody, Nothing, "text/plain")

        'then we create the Html part
        'to embed images, we need to use the prefix 'cid' in the img src value
        Dim htmlBody As String

        htmlBody = "<link href='cid:CSS_EstiloC' rel='stylesheet' type='text/css' />"
        htmlBody += "<link href='cid:CSS_Chile' rel='stylesheet' type='text/css' />"

        htmlBody += "<table width='543' border='0' align='center' cellpadding='0' cellspacing='0'>"
        htmlBody += "<tr>"
        htmlBody += "<td colspan='3' bgcolor='#999999'>"
        htmlBody += "<img alt='' src='cid:Img_1x1' width='1' height='1' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td width='1' bgcolor='#999999'>"
        htmlBody += "<img alt='' src='cid:Img_1x1' width='1' height='1' />"
        htmlBody += "</td>"
        htmlBody += "<td width='541'>"
        htmlBody += "<table width='541' border='0' align='center' cellpadding='0' cellspacing='0'>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<img alt='' src='cid:Img_FullFotoCab' width='541' height='119' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<table width='90%' border='0' align='center' cellpadding='0' cellspacing='1'>"
        htmlBody += "<tr>"
        htmlBody += "<td>&nbsp;</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo1'>&iexcl;Felicitaciones [lblNombreCliente]!</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>&nbsp;</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<div align='justify'>"
        htmlBody += "<span class='Estilo2'>Agradecemos su preferencia al contratar <strong>Seguro FULL AUTO </strong> de Banchile Corredores de Seguros y le informamos que est&aacute; disponible en Banco en L&iacute;nea, su <strong>SOAP 2012</strong> para que sea contratado sin costo para usted."
        htmlBody += "<br />"
        htmlBody += "<br />"
        htmlBody += "El veh&iacute;culo al cual se otorgar&aacute; el <strong>Seguro Obligatorio de Accidentes Personales 2012</strong> (SOAP) sin costo, corresponde a la patente:"
        htmlBody += "<br/>"
        htmlBody += "</span>"
        htmlBody += "</div>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td height='90'>"
        htmlBody += "<div align='center'>"
        htmlBody += "<table width='165' border='0' cellpadding='0' cellspacing='0'>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<img src='cid:Img_Pat1' width='11' height='63' />"
        htmlBody += "</td>"
        htmlBody += "<td>"
        htmlBody += "<table width='154'  border='0' cellpadding='0' cellspacing='0'>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<img src='cid:Img_Pat2' width='154' height='10' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td height='33'>"
        htmlBody += "<div align='center'>"
        htmlBody += "<table width='97%' border='0' cellpadding='0' cellspacing='0'>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<div align='center'>"

        If ColorCliente = "A" Then
            htmlBody += "<span class='Estilo7'>[lblPat1]</span>"
        Else
            htmlBody += "<span class='Estilo9'>[lblPat1]</span>"
        End If

        htmlBody += "</div>"
        htmlBody += "</td>"
        htmlBody += "<td width='9'>"
        htmlBody += "<div align='center'>"
        htmlBody += "<img alt='' src='cid:Img_PatPto' width='9' height='33' />"
        htmlBody += "</div>"
        htmlBody += "</td>"
        htmlBody += "<td class='Estilo7'>"

        If ColorCliente = "A" Then
            htmlBody += "<div align='center'>[lblPat2]</div>"
        Else
            htmlBody += "<div align='center' class='Estilo10'>[lblPat2]</div>"
        End If

        htmlBody += "</td>"
        htmlBody += "<td width='9'>"
        htmlBody += "<img alt='' src='cid:Img_PatAstco' width='9' height='33' />"
        htmlBody += "</td>"
        htmlBody += "<td class='Estilo7'>"

        If ColorCliente = "A" Then
            htmlBody += "<div align='center'>[lblPat3]</div>"
        Else
            htmlBody += "<div align='center' class='Estilo10'>[lblPat3]</div>"
        End If

        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</div>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<img alt='' src='cid:Img_Pat3' width='154' height='20' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</td>"
        htmlBody += "<td>"
        htmlBody += "<img alt='' src='cid:Img_Pat4' width='11' height='63' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</div>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'>Para contratar su p&oacute;liza <strong>SOAP 2012</strong> sin costo debe realizar los siguientes pasos:<br/>&nbsp;</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<table width='93%'  border='0' align='center' cellpadding='0' cellspacing='2'>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'><strong>1)</strong> Ingrese a [lblPortal] </span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'><strong>2)</strong> Ingrese a la Campa&ntilde;a SOAP 2012</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'><strong>3)</strong> Pinche en <strong>Comprar P&oacute;liza</strong></span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'><strong>4)</strong> Ingrese la patente <span id='lblPatente' class='Estilo3'>[lblPatente]</span>&nbsp;y confirme los datos solicitados</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'><strong>5)</strong> Imprima su SOAP &iexcl;Sin costo!</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo6'>&nbsp;</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<span class='Estilo2'>Adicionalmente, puede contratar cuantas p&oacute;lizas SOAP requiera, con cargo a su Cuenta Corriente o Tarjeta de Cr&eacute;dito.</span>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>&nbsp;</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td>"
        htmlBody += "<img alt='' src='cid:Img_FullSoap' width='541' height='119' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"
        htmlBody += "</td>"
        htmlBody += "<td width='1' bgcolor='#999999'>"
        htmlBody += "<img src='cid:Img_1x1' alt='' width='1' height='1' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "<tr>"
        htmlBody += "<td colspan='3' bgcolor='#999999'>"
        htmlBody += "<img src='cid:Img_1x1' alt='' width='1' height='1' />"
        htmlBody += "</td>"
        htmlBody += "</tr>"
        htmlBody += "</table>"

        If NombreCliente.Split(" ").Length >= 3 Then
            htmlBody = htmlBody.Replace("[lblNombreCliente]", StrConv(NombreCliente.Split(" ")(2) & " " & NombreCliente.Split(" ")(0), VbStrConv.ProperCase)) 'sNombre(2) & " " & sNombre(0))
        Else
            htmlBody = htmlBody.Replace("[lblNombreCliente]", Cliente)
        End If

        htmlBody = htmlBody.Replace("[lblPat1]", Mid(Patente, 1, 2))
        htmlBody = htmlBody.Replace("[lblPat2]", Mid(Patente, 3, 2))
        htmlBody = htmlBody.Replace("[lblPat3]", Mid(Patente, 5, 2))

        htmlBody = htmlBody.Replace("[lblPatente]", Patente)

        'OJO: Los comprobantes no dicen nada de reemplazar estos valores según el ColorCliente.
        'Sin embargo la validación ya está lista. Llegar y descomentar
        'If ColorCliente = "A" Then
        '    htmlBody = htmlBody.Replace("[lblPortal]", "www.bancochile.cl")
        'Else
        '    htmlBody = htmlBody.Replace("[lblPortal]", "www.bancoedwards.cl")
        'End If

        htmlBody = htmlBody.Replace("[lblPortal]", "www.bancochile.cl o www.bancoedwards.cl")

        Dim htmlView As Net.Mail.AlternateView = Net.Mail.AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, "text/html")

        Dim MiRecurso As Net.Mail.LinkedResource

        If ColorCliente = "A" Then
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\css\estilocbch.css")
        Else
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\css\estilocedw.css")
        End If

        MiRecurso.ContentId = "CSS_EstiloC"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\css\chile.css")
        MiRecurso.ContentId = "CSS_Chile"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\1x1.gif")
        MiRecurso.ContentId = "Img_1x1"
        htmlView.LinkedResources.Add(MiRecurso)

        If ColorCliente = "A" Then
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\full_foto1.jpg")
            MiRecurso.ContentId = "Img_FullFotoCab"
            htmlView.LinkedResources.Add(MiRecurso)
        Else
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\full_foto1.jpg")
            MiRecurso.ContentId = "Img_FullFotoCab"
            htmlView.LinkedResources.Add(MiRecurso)
        End If

        If ColorCliente = "A" Then
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\full_soap.jpg")
            MiRecurso.ContentId = "Img_FullSoap"
            htmlView.LinkedResources.Add(MiRecurso)
        Else
            MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\full_soap_edwards.jpg")
            MiRecurso.ContentId = "Img_FullSoap"
            htmlView.LinkedResources.Add(MiRecurso)
        End If

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat1.gif")
        MiRecurso.ContentId = "Img_Pat1"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat2.gif")
        MiRecurso.ContentId = "Img_Pat2"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat3.gif")
        MiRecurso.ContentId = "Img_Pat3"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat4.gif")
        MiRecurso.ContentId = "Img_Pat4"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat_pto.gif")
        MiRecurso.ContentId = "Img_PatPto"
        htmlView.LinkedResources.Add(MiRecurso)

        MiRecurso = New Net.Mail.LinkedResource(Server.MapPath(".") & "\\images\pat_astco.gif")
        MiRecurso.ContentId = "Img_PatAstco"
        htmlView.LinkedResources.Add(MiRecurso)

        'add the views
        'mail.AlternateViews.Add(plainView)
        mail.AlternateViews.Add(htmlView)

        'send mail
        SendMail(mail)

    End Sub ' End EmbedImages

    Protected Sub btnConfirmar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirmar.Click
        Dim MiEstado() As String
        MiEstado = AgregarSeguroWS().Split("|")

        GrabarSOAPGratis(MiEstado(0), MiEstado(1))

        'EnviarMail(ColorCliente, Nombre, Patente, Email)

        EnviarMail(ColorCliente, NombreCliente, Patente, Email & "," & "pablo.rodriguez@entersoft.cl") 'OJO: Desarrollo
        'EnviarMail("V", Nombre, Patente, Email & "," & "juan.fernandez@entersoft.cl") 'OJO: Desarrollo

        If ColorCliente = "A" Then
            IrA("frmComprobanteBch.aspx")
        Else
            IrA("frmComprobanteEdw.aspx")
        End If
    End Sub

    Protected Function AgregarSeguroWS() As String
        Dim letrasPatente As String
        Dim numPatente As String

        If IsNumeric(Right(Patente.Replace("-", ""), 4)) Then
            numPatente = Right(Patente.Replace("-", ""), 4)
            letrasPatente = Left(Patente.Replace("-", ""), 2)
        Else
            letrasPatente = Left(Patente.Replace("-", ""), 4)
            numPatente = Right(Patente.Replace("-", ""), 2)
        End If

        Return AgregarSeguroWS(letrasPatente, numPatente)
    End Function

    Protected Function AgregarSeguroWS(ByVal letrasPatente As String, ByVal numPatente As String) As String
        Dim ws As wsConsorcio.ServiciosPatente = New wsConsorcio.ServiciosPatente()
        Dim parm As wsConsorcio.parametros = New wsConsorcio.parametros()
        Dim res As wsConsorcio.resultado = New wsConsorcio.resultado()
        Dim MiRespuesta As String

        parm.letrasPatente = letrasPatente

        parm.numPatente = numPatente

        parm.numPatenteSpecified = True

        Try
            res = ws.agregarPatente(parm)
            MiRespuesta = res.codigo.ToString & "|" & DateTime.Now
        Catch ex As Exception
            MiRespuesta = "-1|" & DateTime.Now
        End Try

        Return MiRespuesta
    End Function

    Protected Function GrabarSOAPGratis(ByVal Estado As Integer, ByVal FechaWS As DateTime) As Boolean
        Return GrabarSOAPGratis(Patente, NroSeguro, RUTCliente, NombreCliente, Marca, Modelo, TipoVehiculo, Anio, Estado, FechaWS, Email)
    End Function

    Protected Function GrabarSOAPGratis(ByVal MiPatente As String, _
                                   ByVal MiNroSeguro As String, _
                                   ByVal MiRut As String, _
                                   ByVal MiNombre As String, _
                                   ByVal MiMarca As String, _
                                   ByVal MiModelo As String, _
                                   ByVal MiTipo As String, _
                                   ByVal MiAnio As Integer, _
                                   ByVal MiEstado As Integer, _
                                   ByVal MiFechaWS As Date, _
                                   ByVal MiEmail As String) As Boolean
        Dim rp As New DotNetResponse.SQLPersistence
        Dim MiRespuesta As Boolean

        rp.Parametro = New String() {MiPatente, MiNroSeguro, MiRut, MiNombre, MiMarca, MiModelo, MiTipo, MiAnio, MiEstado, MiFechaWS, MiEmail}

        rp.StoredProcedure = "SP_PUT_SOAP2012"

        ExecuteSP(rp, True, False)

        MiRespuesta = Not rp.Errores

        rp.Dispose()

        Return MiRespuesta
    End Function
End Class